package com.zcsy.commons.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;

/**
 * 属性文件读取工具类.<br>
 * @author tanghan barbadosliy@163.com
 * @version 2015年5月20日
 * @see PropertiesUtil
 * @since
 */
public class PropertiesUtil {
    
    private static final String PROPERTIES_URL = "config.properties";
    private static final String STRING_TRUE = "true";
    
    public static final String WEB_PAY_NOTIFY_URL = "web_pay_notify_url";
    public static final String WEB_PAY_RETURN_URL = "web_pay_return_rul";
    
    public static final String CAR_INTERFACE = "carInterface";
    
    public static final String SELLER_LOGIN_RUL = "seller_login_url";
	
	/**
	 * 当前解析的属性文件.
	 */
	private Properties properties = null;

	public Properties getProperties() {
		return properties;
	}

	public void setProperties(Properties properties) {
		this.properties = properties;
	}

	/**
	 * 构造函数.
	 */
	@SuppressWarnings("unused")
	private PropertiesUtil() {
	}
	
	/**
	 * 构造函数.
	 * @param propFileClassPath 属性文件的classpaht路径.
	 */
	public PropertiesUtil(final String propFileClassPath) {
		String path = propFileClassPath;
		if (StringUtils.isEmpty(path)) {
			throw new RuntimeException("属性文件的路径参数不能为空");
		}
		if(!path.startsWith("/")) {
			path = "/" + path;
		}
		
		final InputStream in = this.getClass().getResourceAsStream(path);		
		this.properties = new Properties();
		try {
			this.properties.load(in);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 构造函数.
	 * @param propfile 属性文件.
	 */
	public PropertiesUtil(final File propfile) {
		try {
			final InputStream in = new FileInputStream(propfile);
			this.properties = new Properties();
			this.properties.load(in);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 获取属性值.
	 * @param propertyName 属性的key.
	 * @return 属性值.
	 */
	public String getProperty(final String propertyName) {
		return this.properties.getProperty(propertyName);
	}
	
	/**
	 * 修改属性值
	 * @param propertyName 属性的key
	 * @param value 新的属性值
	 */
	public void setProperty(final String propertyName, String value){
		this.properties.setProperty(propertyName, value);
	}
	
	/**
	 * 修改配置文件的邮箱服务器
	 * @param value
	 */
	public void setHostProperty(String value){
		this.properties.setProperty("mailServer_host", value);
	}
	
	/**
	 * 获取邮箱类型
	 * @param mailName
	 * @return
	 */
	public String getMailType(String mailName){
		String type = null;
		if (mailName.contains("@qq")){
			type = "qq";
		}
		else if (mailName.contains("@163")){
			type = "163";
		}
		else if (mailName.contains("@sina")){
			type = "sina";
		}
		else if (mailName.contains("@126")){
			type = "126";
		}
		else if (mailName.contains("@sohu")){
			type = "sohu";
		}
		return type;
	}
	
	/**
	 * 设置相应邮箱服务器
	 * @param mailName
	 * @param hostMailName
	 */
	public void setHostMailName(String mailName, String hostMailName){
		String mailType = getMailType(mailName);
		
		if (mailType.equals("qq")){
			this.setHostProperty("smtp.qq.com");
		}
		else if (mailType.equals("163")){
			this.setHostProperty("smtp.163.com");
		}
		else if (mailType.equals("126")){
			this.setHostProperty("smtp.126.com");
		}
		else if (mailType.equals("sina")){
			this.setHostProperty("smtp.sina.com.cn");
		}
		else if (mailType.equals("sohu")){
			this.setHostProperty("smtp.sohu.com");
		}
	}
	
	/** 
	 * @Title: getBackgroundIp 
	 * @Description: 获取后台IP地址
	 * @param @return     
	 * @return String     
	 * @throws 
	 */
	public static String getBackgroundIp(){
		return new PropertiesUtil("config.properties").getProperty("background_ip");
	}
	
	/** 
	 * @Title: getForegroundIp 
	 * @Description: 获取前台IP地址
	 * @param @return     
	 * @return String     
	 * @throws 
	 */
	public static String getForegroundIp(){
		return new PropertiesUtil("config.properties").getProperty("foreground_ip");
	}
	
	/** 
	 * @Title: getPayReturn 
	 * @Description: 获取同步通知页面路径
	 * @param @return     
	 * @return String     
	 * @throws 
	 */
	public static String getPayReturn(){
		return new PropertiesUtil("config.properties").getProperty("return_url");
	}
	
	/** 
	 * @Title: getPayNotify 
	 * @Description: 获取异步通知页面路径
	 * @param @return     
	 * @return String     
	 * @throws 
	 */
	public static String getPayNotify(){
		return new PropertiesUtil("config.properties").getProperty("notify_url");
	}
	
	/**
	 * 
	 * @Description <br>
	 * 获取订单编号的前缀
	 * @return 订单编号前缀
	 * @author chenshengfang 735569870@qq.com
	 * @date 2015年8月14日
	 * @see
	 */
	public static String getOrderNo(){
        return new PropertiesUtil("config.properties").getProperty("order_no");
    }
	
	/** 
	 * @Title: getSmsOpenOrNot 
	 * @Description: 获取短信是否打开
	 * @param @return     
	 * @return String     
	 * @throws 
	 */
	public static String getSmsOpenOrNot(){
        return new PropertiesUtil("config.properties").getProperty("sms.sendsms.isOpen");
    }
	
	public static void main(String[] args) {
		System.out.println(getDefaultAferSale());
	}
	public static String getDefaultAferSale(){
		return new PropertiesUtil("config.properties").getProperty("after_sale");
	}
	
	/**
	 * 
	 * @Description 获得德邦请求报文的部分信息<br>
	 * @return 请求报文部分信息
	 * @author lixiang 464774362@qq.com
	 * @date 2015年8月24日
	 * @see
	 */
	public static String getCompanyCode(){
		return new PropertiesUtil("config.properties").getProperty("company_code");
	}
	public static String getAppkey(){
		return new PropertiesUtil("config.properties").getProperty("appkey");
	}
	public static String getUrl(){
		return new PropertiesUtil("config.properties").getProperty("url");
	}
	
	/**
	 * 
	 * @Description <br>
	 * 获取买家付款时用来接收短信的第一个手机号
	 * @return 
	 * @author chenshengfang 735569870@qq.com
	 * @date 2015年9月10日
	 * @see
	 */
	public static String getPhone1(){
        return new PropertiesUtil("config.properties").getProperty("phone1");
    }
	
	/**
     * 
     * @Description <br>
     * 获取买家付款时用来接收短信的第二个手机号
     * @return 
     * @author chenshengfang 735569870@qq.com
     * @date 2015年9月10日
     * @see
     */
    public static String getPhone2(){
        return new PropertiesUtil("config.properties").getProperty("phone2");
    }
    
    /**
     * 
     * @Description <br>
     * 获取买家付款时用来接收短信的第三个手机号
     * @return 
     * @author chenshengfang 735569870@qq.com
     * @date 2015年9月10日
     * @see
     */
    public static String getPhone3(){
        return new PropertiesUtil("config.properties").getProperty("phone3");
    }
    
    /**
     * 
     * @Description <br>
     * 获取当前环境是否为测试环境
     * @return 
     * @author chenshengfang 735569870@qq.com
     * @date 2015年9月10日
     * @see
     */
    public static String getIsCeshi(){
        return new PropertiesUtil("config.properties").getProperty("isCeshi");
    }
	
    public static boolean isCeshi(){
        String value = new PropertiesUtil("config.properties").getProperty("isCeshi");
        return STRING_TRUE.equals(value);
    }
    
    public static boolean requestInterface(){
        String value = new PropertiesUtil("config.properties").getProperty(CAR_INTERFACE);
        return STRING_TRUE.equals(value);
    }
    
    /** 
     * @Title: getUnionPayUrl 
     * @Description: 获取银联支付的链接
     * @param @return     
     * @return String     
     * @throws 
     */
    public static String getUnionPayUrl(){
    	return new PropertiesUtil("config.properties").getProperty("union_pay_url");
    }
    
    public static String getUnionMeridNormal(){
    	return new PropertiesUtil("config.properties").getProperty("union_merid_normal");
    }
    
    public static String getUnionMeridNocard(){
    	return new PropertiesUtil("config.properties").getProperty("union_merid_nocard");
    }
    
    public static String getUnionMeridCompany(){
    	return new PropertiesUtil("config.properties").getProperty("union_merid_company");
    }
    
    public static String getunion_notify_url(){
    	return new PropertiesUtil("config.properties").getProperty("union_notify_url");
    }
    
    public static String getunion_return_url(){
    	return new PropertiesUtil("config.properties").getProperty("union_return_url");
    }
    
    public static String getIsLinux(){
    	return new PropertiesUtil("config.properties").getProperty("isLinux");
    }
    
    public static String getDefaultMemName(){
        return new PropertiesUtil("config.properties").getProperty("defaultMemName");
    }
    public static String getWxAppId(){
    	return new PropertiesUtil("config.properties").getProperty("appId");
    }
    public static String getWxAppSecret(){
    	return new PropertiesUtil("config.properties").getProperty("appSecret");
    }
    public static String getWxPartner(){
    	return new PropertiesUtil("config.properties").getProperty("partner");
    }
    public static String getWxPartnerKey(){
    	return new PropertiesUtil("config.properties").getProperty("partnerkey");
    }
    public static String getWxPayNotifyUrl(){
    	return new PropertiesUtil("config.properties").getProperty("wxPay_notify_url");
    }
    public static String getSpbillCreateIp(){
    	return new PropertiesUtil("config.properties").getProperty("spbillCreateIp");
    }
    public static String getAppDownloadUrlBase(){
    	return new PropertiesUtil("config.properties").getProperty("appDownloadUrlBase");
    }
    public static String getFykey(){
    	return new PropertiesUtil("config.properties").getProperty("fykey");
    }
    public static String getapp_union_notify_url(){
    	return new PropertiesUtil("config.properties").getProperty("app_union_notify_url");
    }
    public static String getapp_union_merid(){
    	return new PropertiesUtil("config.properties").getProperty("app_union_merid");
    }
    /*begin app微信支付*/
    public static String getapp_wxPay_notify_url(){
        return new PropertiesUtil("config.properties").getProperty("app_wxPay_notify_url");
    }
    public static String getapp_wxPay_appId(){
        return new PropertiesUtil("config.properties").getProperty("app_wxPay_appId");
    }
    public static String getapp_wxPay_APPSECRET(){
        return new PropertiesUtil("config.properties").getProperty("app_wxPay_APPSECRET");
    }
    public static String getapp_wxPay_MCH_ID(){
        return new PropertiesUtil("config.properties").getProperty("app_wxPay_MCH_ID");
    }
    public static String getapp_wxPay_MCH_ID_key(){
        return new PropertiesUtil("config.properties").getProperty("app_wxPay_MCH_ID_key");
    }
    public static String getapp_wxPay_spbillCreateIp(){
        return new PropertiesUtil("config.properties").getProperty("app_wxPay_spbillCreateIp");
    }
    /*end app微信支付*/
    
    public static String getPropertiesUtilValues(String name) {
        return new PropertiesUtil(PROPERTIES_URL).getProperty(name);
    }
}




